﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using System.Drawing.Drawing2D;
using LPV_POS.mh_pos.mh_cauhinh;
using System.Resources;
using System.Globalization;


namespace LPV_POS.mh_pos
{
    public partial class mh_dmkhuvuc : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        string sMa_Khu_Vuc = "";

        private Point startPoint;
        System.Windows.Forms.Control Control_Current = null;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_dmkhuvuc).Assembly);
                this.grvkhuvuc.GroupPanelText = rm.GetString("danhsachkhuvuc", culture);
                label2.Text = rm.GetString("ten", culture);
                bton_tim.Text = rm.GetString("tim", culture);
                label1.Text = rm.GetString("ten", culture);
                this.page_khuvuc.Text = rm.GetString("khuvuc_1", culture);
                this.page_ban.Text = rm.GetString("ban_1", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_sua.Text = rm.GetString("sua", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);

                btnThemBan.Text = rm.GetString("themban", culture);
                groupControl1.Text = rm.GetString("sodoban", culture);
                chkX.Text = rm.GetString("rong", culture);
                chkY.Text = rm.GetString("cao", culture);
                lblForeColor.Text = rm.GetString("mauchu", culture);
                btnHinh.Text = rm.GetString("hinh", culture);
                label3.Text = rm.GetString("dong", culture);
                label4.Text = rm.GetString("cot", culture);
                btnTuSapXep.Text = rm.GetString("sapxepban", culture);
                btnLuu.Text = rm.GetString("luusodo", culture);
                lbel_tencongty.Text = rm.GetString("cauhinhsodoban", culture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion
        
        #region "   Khu Vuc"
        // select danh sach nhom chu hh
        private DataTable f_selectdsnhomchuhh()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuvuc", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach nhom chu hh co dieu kien
        private DataTable f_selecttenkhuvucdk(string tenkhuvuc)
        {

            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME" };
            o = new object[] { 1, 2, tenkhuvuc };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuvuc", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // insert them khu vuc
        private int f_insertkhuvuc(string tenkhuvuc)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME"};
            o = new object[] { 2, 1, tenkhuvuc };
            int ketqua = tv.SP_Update("sp_mh_dmkhuvuc", s, o);
            return ketqua;
        }

        // update mot don vi tinh
        private int f_updatekhuvuc(string makhuvuc, string tenkhuvuc)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME" };
            o = new object[] { 3, 1, makhuvuc, tenkhuvuc };
            int ketqua = tv.SP_Update("sp_mh_dmkhuvuc", s, o);
            return ketqua;
        }

        // xoa khu vuc
        private int f_deletekhuvuc(string makhuvuc)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, makhuvuc };
            int ketqua = tv.SP_Update("sp_mh_dmkhuvuc", s, o);
            return ketqua;
        }
        public mh_dmkhuvuc()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
        }

        private void mh_dmkhuvuc_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);
            grdkhuvuc.DataSource = f_selectdsnhomchuhh();
        }

        private void grid_khuvuc_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
            int chiso = grvkhuvuc.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grvkhuvuc.FocusedColumn;
            txt_tenkhuvuc.Text = grvkhuvuc.GetRowCellDisplayText(chiso, col_tenkhuvuc) + "";

            bton_luu.Enabled = false;
            bton_tenkhuvuc.Enabled = false;

            //
            DataRow drKhuVuc = grvkhuvuc.GetDataRow(grvkhuvuc.FocusedRowHandle);
            sMa_Khu_Vuc = "";
            if (drKhuVuc != null)
            {
                sMa_Khu_Vuc = drKhuVuc["OBJID"] + "";
            }
            this.GetDataTable();
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tenkhuvuc.Text = "";
            bton_luu.Enabled = true;
            bton_tenkhuvuc.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_luu.Enabled = true;
            bton_tenkhuvuc.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (grdkhuvuc.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;

            string makhuvuc = grvkhuvuc.GetRowCellValue(grvkhuvuc.FocusedRowHandle, col_makhuvuc) + "";

            #region "   Kiem tra hop le truoc khi xoa"
            thuvien othuvien = new thuvien();
            //BH_PHIEU_TMP
            string sSQL = "SELECT 1 FROM BH_PHIEU p WHERE p.DM_BAN_MA IN(SELECT b.OBJID FROM DM_BAN b WHERE b.MAKHUVUC = '" + makhuvuc + "')";
            DataTable tbabc = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL"}, new object[] { sSQL});
            if (tbabc.Rows.Count > 0)
            {
                MessageBox_LPV.Show("Khu vực có bàn(phòng) đã sử dụng\nkhông thể xóa.","Thanh toán",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            //BH_PHIEU_TMP
            sSQL = "SELECT 1 FROM BH_PHIEU_TMP p WHERE p.DM_BAN_MA IN(SELECT b.OBJID FROM DM_BAN b WHERE b.MAKHUVUC = '" + makhuvuc + "')";
            tbabc = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            if (tbabc.Rows.Count > 0)
            {
                MessageBox_LPV.Show("Khu vực có bàn(phòng) đã sử dụng, không thể xóa.", "Tạm tính", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //DM_BAN_DAT_BAN
            sSQL = "SELECT 1 FROM DM_BAN_DAT_BAN db WHERE db.DM_BAN_ID IN(SELECT b.OBJID FROM DM_BAN b WHERE b.MAKHUVUC = '" + makhuvuc + "')";
            tbabc = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            if (tbabc.Rows.Count > 0)
            {
                MessageBox_LPV.Show("Khu vực có bàn(phòng) đã được đặt, không thể xóa.", "Đặt bàn(phòng)", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            #endregion

            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletekhuvuc(makhuvuc);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    return;
                }

                grdkhuvuc.DataSource = f_selectdsnhomchuhh();
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            int ketqua;

            if (txt_tenkhuvuc.Text == "")       // không thể luu neu chua nhap ten don vi tinh
                return;
            int chiso = -1;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertkhuvuc(txt_tenkhuvuc.Text);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                chiso = ((ColumnView)grdkhuvuc.Views[0]).FocusedRowHandle;
                string makhuvuc = grvkhuvuc.GetRowCellValue(grvkhuvuc.FocusedRowHandle, col_makhuvuc).ToString();
                ketqua = f_updatekhuvuc(makhuvuc, txt_tenkhuvuc.Text);
                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // luoi_nhomchuhh.DataSource = f_selectdsnhomchuhh();
            }
            grdkhuvuc.DataSource = null;
            grdkhuvuc.DataSource = f_selectdsnhomchuhh();
            if (Variable.str_trangthaithemsua == "sua" && chiso != -1)
            {
                grvkhuvuc.SelectRow(chiso);
                grvkhuvuc.FocusedRowHandle = chiso;

            }
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            string tenkhuvuc = "";
            if (txt_timtenkhuvuc.Text != "")
                tenkhuvuc = "'%" + txt_timtenkhuvuc.Text + "%'";
            else
                tenkhuvuc = txt_timtenkhuvuc.Text;
            grdkhuvuc.DataSource = null;
            grdkhuvuc.DataSource = f_selecttenkhuvucdk(tenkhuvuc);
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
                this.Close();
        }

        private void bton_tenkhuvuc_Click(object sender, EventArgs e)
        {
            //mh_chu frm_chu = new mh_chu();
            //frm_chu.ControlBox = false;
            //Variable.str_position_frmchu = "mhdmkhuvuc_bton_tenkhuvuc";
            //frm_chu.ShowDialog();
            //txt_tenkhuvuc.Text = Variable.str_chu;
            txt_tenkhuvuc.Text = new cpublic().loadformchu_point(300,250);
            
        }

        private void grid_khuvuc_DoubleClick(object sender, EventArgs e)
        {
            tab_khuvuc.SelectedTabPage = page_ban;
        }
        #endregion

        #region "   Ban / Phong"
        
        #region "   btnLuu_Click"
        private void btnLuu_Click(object sender, EventArgs e)
        {
            int i = 0;
            cpublic ocpublic = new cpublic();
            foreach (System.Windows.Forms.Control ctrl in pnlSoDoBan.Controls)
            {
                //this.luuControl(ctrl);
                ocpublic.luuControl_Format("sodoban" + ctrl.Name, ctrl);
                i++;
            }
            //MessageBox_LPV.Show("Lưu thành công");
            MessageBox.Show("Lưu thành công", i + "");
          //  tab_khuvuc.SelectedTabPage = page_khuvuc;
        }
        #endregion

        

        #region "   DataTable   GetDataTable"
        void GetDataTable()
        {
            DataTable tb = new DataTable();
            thuvien oclsData = new thuvien();

            tb = oclsData.SP_SelectDataTable("sp_mh_banhang",
                                             new string[] { "@flag", "@MAKHUVUC" },
                                             new object[] { 1, sMa_Khu_Vuc });
            pnlSoDoBan.Controls.Clear();
            string sBanMa = "";
            foreach (DataRow dr in tb.Rows)
            {
                if (dr["ban_ma"] + "" != sBanMa)
                {
                    sBanMa = dr["ban_ma"] + "";
                    System.Windows.Forms.Control ctrl = this.getPanel(dr);
                    ctrl.Location = ctrl.Location;

                    this.SetHinhAnh(dr["HINH_NEN"] + "", ctrl, int.Parse("0" + dr["TRANGTHAI"] + ""));
                    pnlSoDoBan.Controls.Add(ctrl);
                }
            }
            lblCount.Text = tb.Rows.Count + "";
        }
        #endregion

        #region "   getLabel"
        Label getLabel(System.Windows.Forms.Control _parent, DataRow _drBan)
        {
            Label lblbanA = new Label();
            lblbanA.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            lblbanA.BackColor = System.Drawing.Color.Transparent;
            lblbanA.Font = new System.Drawing.Font("Times New Roman", 12.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lblbanA.Location = new System.Drawing.Point(0, 0);
            lblbanA.Name = "lblBan" + _drBan["MAKHUVUC"] + _drBan["ban_ma"] + "";
            lblbanA.Size = new System.Drawing.Size(_parent.Size.Width + 1, _parent.Size.Height + 1);
            lblbanA.TabIndex = 1;
            lblbanA.Text = _drBan["ban_ten"] + "\n(" + _drBan["LOAIDICHVU_TEN"] + ")";
            lblbanA.Tag = _drBan["ban_ma"] + "";
            lblbanA.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            lblbanA.BorderStyle = BorderStyle.None;
            lblbanA.ForeColor = _parent.ForeColor;

            this.Add_Event(lblbanA);
            //

            return lblbanA;
        }
        #endregion

        ToolTip toolTip1 = new ToolTip();

        #region "   getPanel"
        Panel getPanel(DataRow _drBan)
        {
            Panel pnlbanA = new Panel();
            pnlbanA.BackColor = this.getBackColor(_drBan["TRANGTHAI"] + "");
            pnlbanA.Font = new System.Drawing.Font("Times New Roman", 12.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            pnlbanA.Location = new System.Drawing.Point(35, 8);
            pnlbanA.Name = "lblBan" + _drBan["MAKHUVUC"] + _drBan["ban_ma"] + "";
            pnlbanA.Size = new System.Drawing.Size(100, 100);
            pnlbanA.TabIndex = 1;
            pnlbanA.Text = _drBan["ban_ten"] + "";

            pnlbanA.Tag = _drBan["ban_ma"] + "";
            //pnlbanA.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //pnlbanA.BorderStyle = BorderStyle.FixedSingle;
            pnlbanA.BackgroundImageLayout = ImageLayout.Stretch;
            this.Add_Event(pnlbanA);
            
            new cpublic().loadControl_Format("sodoban" + pnlbanA.Name, pnlbanA);
            //
            toolTip1.SetToolTip(pnlbanA, _drBan["ban_ten"] + "");

            Label lbltmp = this.getLabel(pnlbanA, _drBan);
            lbltmp.Region = pnlbanA.Region;
            pnlbanA.Controls.Add(lbltmp);
            return pnlbanA;
        }
        #endregion

        #region "   getBackColor"
        Color getBackColor(string _TrangThai)
        {
            if (_TrangThai == "0")//Chưa có khách
                return Color.White;
            else if (_TrangThai == "1")//Có khách
                return Color.SpringGreen;
            else //if (_TrangThai == "2")//'Đã đặt trước
                return Color.Yellow;
        }
        #endregion

        #region "   SetHinhAnh"
        void SetHinhAnh(string sSaveFolder, System.Windows.Forms.Control ctrl, int _thang_thai)
        {
            if (sSaveFolder == "")
                sSaveFolder = "Ban_Cafe";
            if (ctrl.Region != null)
            { //Hinh tron
                string sFileTron = Application.StartupPath + "\\hinh_anh\\" + sSaveFolder + "\\mat_ban_Tron_" + (_thang_thai + 1) + ".PNG";
                if (new System.IO.FileInfo(sFileTron).Exists)
                    ctrl.BackgroundImage = Image.FromFile(sFileTron);
            }
            else
            {
                //Hình Vuông
                string sFileVuong = Application.StartupPath + "\\hinh_anh\\" + sSaveFolder + "\\mat_ban_Vuong_" + (_thang_thai + 1) + ".PNG";
                if (new System.IO.FileInfo(sFileVuong).Exists)
                    ctrl.BackgroundImage = Image.FromFile(sFileVuong);

                //Hình Đứng
                sFileVuong = Application.StartupPath + "\\hinh_anh\\" + sSaveFolder + "\\dung_" + (_thang_thai + 1) + ".PNG";
                if (new System.IO.FileInfo(sFileVuong).Exists && ctrl.Size.Width < ctrl.Size.Height)
                    ctrl.BackgroundImage = Image.FromFile(sFileVuong);

                //Hình Ngang
                sFileVuong = Application.StartupPath + "\\hinh_anh\\" + sSaveFolder + "\\ngang_" + (_thang_thai + 1) + ".PNG";
                if (new System.IO.FileInfo(sFileVuong).Exists && ctrl.Size.Width >= ctrl.Size.Height)
                    ctrl.BackgroundImage = Image.FromFile(sFileVuong);
            }
        }
        #endregion

        #region "   1.Add Add_MouseEvent"
        void Add_Event(System.Windows.Forms.Control ctrl)
        {
            ctrl.Click += new EventHandler(pic_MouseClick);
            ctrl.MouseMove += new MouseEventHandler(pic_MouseMove);
            ctrl.MouseDown += new MouseEventHandler(pic_MouseDown);
        }
        #region "   pic_MouseDown"
        private void pic_MouseDown(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.Control oControl = (System.Windows.Forms.Control)sender;
            startPoint = e.Location;
            //picPoint = oControl.Location;
            oControl.BringToFront();
        }
        #endregion

        #region "   pic_MouseMove"
        private void pic_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                System.Windows.Forms.Control oControl = (System.Windows.Forms.Control)sender;
                //oControl.Location = new Point(oControl.Left + e.X - startPoint.X, oControl.Top + e.Y - startPoint.Y);
                if (oControl.ToString().IndexOf("Label") >= 0)
                {
                    oControl.Parent.Location = new Point(oControl.Parent.Left + e.X - startPoint.X, oControl.Parent.Top + e.Y - startPoint.Y);
                }
            }
        }
        #endregion

        #region "   pic_MouseClick"
        void pic_MouseClick(object sender, EventArgs e)
        {
            if (sender != null)
            {
                foreach (System.Windows.Forms.Control ctrChild in pnlSoDoBan.Controls)
                    if (pnlSoDoBan.Controls.Contains(ctrChild))
                        ((Panel)ctrChild).BorderStyle = BorderStyle.None;

                System.Windows.Forms.Control ctrl = (System.Windows.Forms.Control)sender;
                if (pnlSoDoBan.Controls.Contains(ctrl))
                {
                    Control_Current = (System.Windows.Forms.Control)sender;
                    ((Panel)Control_Current).BorderStyle = BorderStyle.FixedSingle;
                    btnHinh.Text = ctrl.Region == null ? "Hình Tròn" : "Hình Vuông";
                    chkX.Text = chkX.Tag + "(" + Control_Current.Size.Width + ")";
                    chkY.Text = chkY.Tag + "(" + Control_Current.Size.Height + ")";
                    return;
                }
                foreach (System.Windows.Forms.Control ctrlTmp in pnlSoDoBan.Controls)
                {
                    if (ctrlTmp.Controls.Contains(ctrl))
                    {
                        Control_Current = (System.Windows.Forms.Control)sender;
                        ((Panel)ctrlTmp).BorderStyle = BorderStyle.FixedSingle;
                        btnHinh.Text = ctrl.Region == null ? "Hình Tròn" : "Hình Vuông";
                        chkX.Text = chkX.Tag + "(" + Control_Current.Parent.Size.Width + ")";
                        chkY.Text = chkY.Tag + "(" + Control_Current.Parent.Size.Height + ")";
                        return;
                    }
                }


            }
        }
        #endregion
        #endregion

        #region "   getFontStyle"
        private FontStyle getFontStyle(string FontStyleName)
        {
            if (FontStyleName.IndexOf("Bold") >= 0 && FontStyleName.IndexOf("Italic") >= 0)
                return (FontStyle)((FontStyle.Bold | FontStyle.Italic));
            else if (FontStyleName.IndexOf("Bold") >= 0)
                return FontStyle.Bold;
            else if (FontStyleName.IndexOf("Italic") >= 0)
                return FontStyle.Italic;
            else
                return FontStyle.Regular;
        }
        #endregion

        #region "   Refresh"
        void Refresh(System.Windows.Forms.Control ctrl)
        {
            if (ctrl.ToString().IndexOf("Label") >= 0)
            {
                if (ctrl.Parent.Region != null)
                    this.convert2LabelHinhTron(ctrl.Parent);
            }
            else if (ctrl.Region != null)
                this.convert2LabelHinhTron(ctrl);
        }
        #endregion

        #region "   btnSize_Tang_Click"
        private void btnSize_Tang_Click(object sender, EventArgs e)
        {            
            string sKey = ((System.Windows.Forms.Control)sender).Tag + "";
            //int x = iSize_Double * (chkX.Checked ? 2 : 0) * (sKey == "+" ? 1 : -1);
            //int y = iSize_Double * (chkY.Checked ? 2 : 0) * (sKey == "+" ? 1 : -1);
            int x = 10 * (sKey == "+" ? 1 : -1) * (chkX.Checked ? 1 : 0);
            int y = 10 * (sKey == "+" ? 1 : -1) * (chkY.Checked ? 1 : 0);

            this.Control_EditSize(Control_Current, x, y);
        }
        #endregion

        #region "   Control_EditSize.................................................."
        void Control_EditSize(System.Windows.Forms.Control ctrl, int _x, int _y)
        {
            if (ctrl == null)
                return;
            try
            {
                int x = ctrl.Size.Width + _x;
                int y = ctrl.Size.Height + _y;

                if (ctrl.ToString().IndexOf("Label") >= 0)
                {
                    if (ctrl.Parent != null)
                    {
                        ctrl.Size = new Size(x - 2, y - 2);

                        ctrl.Parent.Size = new Size(ctrl.Parent.Size.Width + _x, ctrl.Parent.Size.Height + _y);
                        //this.convert2LabelHinhTron(ctrl.Parent);
                        chkX.Text = chkX.Tag + "(" + ctrl.Parent.Size.Width + ")";
                        chkY.Text = chkY.Tag + "(" + ctrl.Parent.Size.Height + ")";
                    }
                }
                else
                {
                    ctrl.Size = new Size(x, y);
                    chkX.Text = chkX.Tag + "(" + x + ")";
                    chkY.Text = chkY.Tag + "(" + y + ")";
                }
                this.Refresh(ctrl);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region "   convert2LabelHinhTron"
        void convert2LabelHinhTron(System.Windows.Forms.Control ctrl)
        {
            GraphicsPath Gp = new GraphicsPath();
            Gp.AddEllipse(0, 0, ctrl.Size.Width - 1, ctrl.Size.Height - 1);
            ctrl.Region = new Region(Gp);
            Gp.Dispose();
            this.Refresh();
        }
        #endregion

        #region "   convertHinhTron2LabelHinhVuong"
        void convertHinhTron2LabelHinhVuong(System.Windows.Forms.Control ctrl)
        {
            ctrl.Region = null;
            this.Refresh();
        }
        #endregion

        #region "   btnHinh_Click"
        private void btnHinh_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Control ctrtemp = Control_Current;
            if (!pnlSoDoBan.Controls.Contains(ctrtemp))
                foreach (System.Windows.Forms.Control ctrlTmp in pnlSoDoBan.Controls)
                {
                    if (ctrlTmp.Controls.Contains(ctrtemp))
                    {
                        ctrtemp = ctrlTmp;
                    }
                }
            if (btnHinh.Text == "Hình Tròn")
            {//Hình Tròn ==> Hình Vuông
                btnHinh.Text = "Hình Vuông";
                this.convert2LabelHinhTron(ctrtemp);
                //9999
                this.SetHinhAnh("",ctrtemp, 1);
            }
            else if (btnHinh.Text == "Hình Vuông")
            {//Hình Vuông ==> Hình Tròn
                btnHinh.Text = "Hình Tròn";
                this.convertHinhTron2LabelHinhVuong(ctrtemp);
                //9999
                this.SetHinhAnh("",ctrtemp, 1);
            }
        }
        #endregion

       
        #endregion
              
        
        private void btnThemBan_Click(object sender, EventArgs e)
        {
            this.ShowForm_DMBan();
        }

        void ShowForm_DMBan()
        {
            if (grvkhuvuc.FocusedRowHandle < 0)
                return;
            DataRow drKhuVuc = grvkhuvuc.GetDataRow(grvkhuvuc.FocusedRowHandle);
            sMa_Khu_Vuc = "";
            if (drKhuVuc != null)
                sMa_Khu_Vuc = drKhuVuc["OBJID"] + "";
            if (sMa_Khu_Vuc + "" == "")
            {
                MessageBox_LPV.Show("Chọn khu vực");
                return;
            }
            mh_dmban omh_dmban = new mh_dmban(sMa_Khu_Vuc);
            Boolean iupdate = omh_dmban.ShowForm();
            if (iupdate)
                this.grid_khuvuc_FocusedRowChanged(null, null);
        }

        private void mh_dmkhuvuc_FormClosing(object sender, FormClosingEventArgs e)
        {
            //mh_danhsachdanhmuc omh_danhsachdanhmuc = (mh_danhsachdanhmuc)this.ParentForm;
            //omh_danhsachdanhmuc.bar3.Visible = true;
        }

        private void lblForeColor_Click(object sender, EventArgs e)
        {
            if (Control_Current != null)
            {
                //ColorDialog cd = new ColorDialog();
                //cd.AnyColor = false;
                //cd.SolidColorOnly = true;
                //cd.Color = Control_Current.BackColor;// Color.DarkBlue;  // sets the custom color
                //if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                //{
                //    Control_Current.ForeColor = cd.Color;
                //    if (Control_Current.ToString().IndexOf("Label") >= 0)
                //    {
                //        Control_Current.Parent.ForeColor = cd.Color;                        
                //    }
                //}
                mh_color omh_color = new mh_color();
                Color _MauChu = omh_color.ShowForm();
                if (omh_color.DialogResult==DialogResult.OK)
                {
                    Control_Current.ForeColor = _MauChu;
                    if (Control_Current.ToString().IndexOf("Label") >= 0)
                    {
                        Control_Current.Parent.ForeColor = _MauChu;
                    }
                }
            }
        }

        private void txtdong_TextChanged(object sender, EventArgs e)
        {
            mh_so omh_so = new mh_so();            
            omh_so.ShowDialog();
            if (omh_so.DialogResult == DialogResult.OK)
            {
                if (new cpublic().IsNumeric(omh_so.txt_ketqua.Text))
                    txtdong.Text = omh_so.txt_ketqua.Text;
            }
        }

        private void txtcot_TextChanged(object sender, EventArgs e)
        {
            mh_so omh_so = new mh_so();
            omh_so.ShowDialog();
            if (omh_so.DialogResult == DialogResult.OK)
            {
                if (new cpublic().IsNumeric(omh_so.txt_ketqua.Text))
                    txtcot.Text = omh_so.txt_ketqua.Text;
            }
        }

        private void btnTuSapXep_Click(object sender, EventArgs e)
        {
            try
            {
                int khungx = 800;
                int khungy = 500;

                int banx = 100;//chieu dai ban
                int bany = 100;//chieu rong ban

                int dong = 1;//dòng
                int.TryParse(txtdong.Text, out dong);

                int cot = 1;//cột
                int.TryParse(txtcot.Text, out cot);

                int KCx = (khungx - banx * cot) / cot;//khoang cách x
                int KCy = (khungy - bany * dong) / dong;//khoang cach y

                int x = 1;//dong thu x
                int y = 1;//cot thu y
                for (int i = 0; i < pnlSoDoBan.Controls.Count; i++)
                {// 35, 10
                    string sName = pnlSoDoBan.Controls[i].Name;
                    pnlSoDoBan.Controls[i].Location = new Point(20 + (y - 1) * 110 + KCx * (y - 1), 35 + (x - 1) * 120 + KCy * (x - 1));

                    if (y >= cot)
                    {
                        y = 1;
                        x = x + 1;
                    }
                    else
                        y = y + 1;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void btnMauBan_Click(object sender, EventArgs e)
        {
            new frmMau_Ban().ShowDialog();
        }

        private void page_khuvuc_Paint(object sender, PaintEventArgs e)
        {

        }


    }
}